/*
 * @Author: w
 * @Date: 2022-03-01 16:17:05
 * @Description: 剑指 Offer 28. 对称的二叉树
 */

// 分层
// 每一层数组不对称则 返回false

/**
 * Definition for a binary tree node.
 * function TreeNode(val) {
 *     this.val = val;
 *     this.left = this.right = null;
 * }
 */
/**
 * @param {TreeNode} root
 * @return {boolean}
 */
var isSymmetric = function (root) {
  if (!root) return false;
  const queue = [];

  while (queue.length) {
    const arr = [];
    for (let i = queue.length; i > 0; i--) {
      const cur = queue[i];
      if (cur) {
        arr.push(cur.val);
      } else {
        arr.push(null);
      }
      if (cur) queue.push(cur.left);
      if (cur) queue.push(cur.right);
    }
    const length = arr.length;
    if (length > 1) {
      for (let j = 0; j < length / 2; j++) {
        if (arr[j] != arr[length - j - 1]) return false;
      }
    }
  }
  return true;
};
